package com.mdd.system.controller;

import com.alibaba.fastjson2.JSONArray;
import com.mdd.common.aop.CurrentUser;
import com.mdd.common.aop.NotPower;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validate.IdValidate;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.system.entity.SystemAuthAdmin;
import com.mdd.system.service.ISystemAuthDeptService;
import com.mdd.system.validate.SystemDeptCreateValidate;
import com.mdd.system.validate.SystemDeptSearchValidate;
import com.mdd.system.validate.SystemDeptUpdateValidate;
import com.mdd.system.vo.SystemAuthAdminInformVo;
import com.mdd.system.vo.SystemAuthDeptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/system/dept")
@Api(tags = "系统部门管理")
public class SystemAuthDeptController {

    @Resource
    ISystemAuthDeptService iSystemAuthDeptService;

    @NotPower
    @GetMapping("/all")
    @ApiOperation(value="部门所有")
    public AjaxResult<List<SystemAuthDeptVo>> all() {
        List<SystemAuthDeptVo> list = iSystemAuthDeptService.all();
        return AjaxResult.success(list);
    }

    @NotPower
    @GetMapping("/list")
    @ApiOperation(value="部门列表")
    public AjaxResult<JSONArray> list(@Validated SystemDeptSearchValidate searchValidate, @CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
        JSONArray list = iSystemAuthDeptService.list(searchValidate,systemAuthAdmin);
        return AjaxResult.success(list);
    }
    @NotPower
    @GetMapping("/listPage")
    @ApiOperation(value="部门列表(分页)")
    public AjaxResult<PageResult<SystemAuthDeptVo>> listPage(@Validated PageValidate pageValidate,
                                                                @Validated SystemDeptSearchValidate searchValidate) {
        PageResult<SystemAuthDeptVo> list = iSystemAuthDeptService.listPage(pageValidate,searchValidate);
        return AjaxResult.success(list);
    }

    @NotPower
    @GetMapping("/listOP")
    @ApiOperation(value="OP列表")
    public AjaxResult<PageResult<SystemAuthDeptVo>> listOP(@Validated PageValidate pageValidate,
                                                             @Validated SystemDeptSearchValidate searchValidate) {
        PageResult<SystemAuthDeptVo> list = iSystemAuthDeptService.listOP(pageValidate,searchValidate);
        return AjaxResult.success(list);
    }

    @NotPower
    @GetMapping("/listCP")
    @ApiOperation(value="CP列表")
    public AjaxResult<PageResult<SystemAuthDeptVo>> listCP(@Validated PageValidate pageValidate,
                                                           @Validated SystemDeptSearchValidate searchValidate) {
        PageResult<SystemAuthDeptVo> list = iSystemAuthDeptService.listCP(pageValidate,searchValidate);
        return AjaxResult.success(list);
    }
    @GetMapping("/detail")
    @ApiOperation(value="部门详情")
    public AjaxResult<SystemAuthDeptVo> detail(@Validated @IDMust() @RequestParam("id") Long id) {
        SystemAuthDeptVo vo = iSystemAuthDeptService.detail(id);
        return AjaxResult.success(vo);
    }

    @NotPower
    @PostMapping("/add")
    @ApiOperation(value="部门新增")
    public AjaxResult<Object> add(@Validated @RequestBody SystemDeptCreateValidate createValidate, @CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
        Assert.notNull(createValidate.getPid(),"父层级id不能为空！" );
        iSystemAuthDeptService.add(createValidate,systemAuthAdmin);
        return AjaxResult.success();
    }
    @NotPower
    @PostMapping("/addOP")
    @ApiOperation(value="OP新增")
    public AjaxResult<Object> addOP(@Validated @RequestBody SystemDeptCreateValidate createValidate) {
        iSystemAuthDeptService.addOP(createValidate);
        return AjaxResult.success();
    }
    @NotPower
    @PostMapping("/addCP")
    @ApiOperation(value="CP新增")
    public AjaxResult<Object> addCP(@Validated @RequestBody SystemDeptCreateValidate createValidate) {
        iSystemAuthDeptService.addCP(createValidate);
        return AjaxResult.success();
    }
    @NotPower
    @PostMapping("/edit")
    @ApiOperation(value="部门编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody SystemDeptUpdateValidate updateValidate) {
        Assert.notNull(updateValidate.getPid(),"父层级id不能为空！" );
        iSystemAuthDeptService.edit(updateValidate);
        return AjaxResult.success();
    }
    @NotPower
    @PostMapping("/editOP")
    @ApiOperation(value="部门编辑")
    public AjaxResult<Object> editOP(@Validated @RequestBody SystemDeptUpdateValidate updateValidate) {
        iSystemAuthDeptService.editOP(updateValidate);
        return AjaxResult.success();
    }
    @NotPower
    @PostMapping("/editCP")
    @ApiOperation(value="部门编辑")
    public AjaxResult<Object> editCP(@Validated @RequestBody SystemDeptUpdateValidate updateValidate) {
        iSystemAuthDeptService.editCP(updateValidate);
        return AjaxResult.success();
    }
    @NotPower
    @PostMapping("/del")
    @ApiOperation(value="部门删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iSystemAuthDeptService.del(idValidate.getId());
        return AjaxResult.success();
    }

    @NotPower
    @GetMapping("/claimList")
    @ApiOperation(value="认领部门列表")
    public AjaxResult<List<SystemAuthDeptVo>> claimList( @CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
        List<SystemAuthDeptVo> list = iSystemAuthDeptService.claimList(systemAuthAdmin);
        return AjaxResult.success(list);
    }
    @NotPower
    @GetMapping("/allocationList")
    @ApiOperation(value="分配部门列表")
    public AjaxResult<List<SystemAuthDeptVo>> allocationList( @CurrentUser SystemAuthAdminInformVo systemAuthAdmin) {
        List<SystemAuthDeptVo> list = iSystemAuthDeptService.allocationList(systemAuthAdmin);
        return AjaxResult.success(list);
    }
}
